<?php namespace Install\Controller;

use Hdphp\Controller\Controller;
use Pdo;
//测试控制器
class InstallController extends Controller{

	//构造函数
	public function __init()
	{
        if(is_file('public/lock.php'))
        {
            View::make('lock');
            exit;
        }
	}

    //动作
    public function setup()
    {
       View::make();
    }

    public function permit()
        {
           View::make();
        }

    public function environment()
    {

            $env['fun']['PDO'] = class_exists('Pdo')?true:false;
            $env['fun']['GD'] = function_exists('imagecreate')?true:false;
            $env['fun']['mb_string'] = function_exists('mb_substr')?true:false;

            $env['dir']['storage'] = is_writable('storage')?'OK':'必须有写权限';
            $env['dir']['backup'] = is_writable('backup')?'OK':'必须有写权限';

            View::with('env',$env);
            View::make();


    }

    public function database()
    {
        if (IS_POST)
        {
            $data = require 'config/database.php';
            $data = array_merge($data,$_POST);

            if(!@mysql_connect($data['host'],$data['user'],$data['password']))
            {

                $this->error('数据库连接错误');
            }
            $sql = "create database if not exists {$data['database']}";
            if(!mysql_query($sql))
            {

                $this->error('数据库连接错误');
            }

            file_put_contents('config/database.php',"<?php \nreturn ".var_export($data,true).";?>");

            go(U('finish'));


        }
        else
        {
            View::make();
        }

    }

    public function finish()
    {
        require "app/Install/data/structure.php";

        foreach (glob('app/Install/data/*') as $f) {
            if(preg_match('@\w+_1.php$@', $f))
            {
                require $f;
            }
        }

        file_put_contents('public/lock.php','');
       View::make();
    }

}
